package pers.cz.util;

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @program: PostGirl-panent
 * @description: 字符串压缩工具
 * @author: Cheng Zhi
 * @create: 2022-10-04 13:39
 **/
public class StringCompressUtils {

    /**
     * 字符串压缩
     * @param str
     * @return
     * @throws IOException
     */
    public static String gzip(String str) throws IOException {

        if (str == null || str.isEmpty()) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (GZIPOutputStream gzip = new GZIPOutputStream(out);) {
            gzip.write(str.getBytes());
        }

        return Base64.encodeBase64String(out.toByteArray());
    }

    /**
     * 字符串解压
     * @param compressedStr
     * @return
     * @throws IOException
     */
    public static String gunzip(String compressedStr) throws IOException {

        if (compressedStr == null || compressedStr.isEmpty()) {
            return compressedStr;
        }
        byte[] compressed = Base64.decodeBase64(compressedStr);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(compressed);
        try (GZIPInputStream ginzip = new GZIPInputStream(in);) {
            byte[] buffer = new byte[4096];
            int len = -1;
            while ((len = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
        }
        return out.toString();
    }
}
